"use strict";

var prefix = ctx + "md/job";
var a;
$().ready(function() {
	validateRule();
	$(".source-second-select").chosen({});
	$(".target-second-select").chosen({});
	$(".technical-select").chosen({});
	
	// 源数据字段变化联动默认值变化
	$("#jobTableforSen").on("click", ".active-result", function() {
		let html = "";
		a = $(this);
		let targetColumnComment = $(this).parents("tr").find("td:last-child").html();
		let sourceColumnComment = $(this).parents("td").find('select[name=sourceColumnComment]').find("option:selected").data('columnComment');
		if(targetColumnComment != sourceColumnComment){
			html = "<input class='form-control' type='text'/>";
		}else{
			html = "-";
		}
		$(this).parents("td").next().html(html);
	});
	
	
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

/**
 * 保存
 * @returns
 */
function save() {
	var jobType = $("#jobType").val();
	if (jobType == '2') { // 同步作业
		var sourceMetaType = $("#sourceMetaType").val();
		if (sourceMetaType == '') {
			layer.msg("请选择源表类型");
			return;
		}
		var sourceTable = $("#sourceTable").val();
		if (sourceTable == '') {
			layer.msg("请选择源表");
			return;
		}
		var targetMetaType = $("#targetMetaType").val();
		if (targetMetaType == '') {
			layer.msg("请选择目标表类型");
			return;
		}
		var targetTable = $("#targetTable").val();
		if (targetTable == '') {
			layer.msg("请选择目标表");
			return;
		}

		var arr = [];
		var trs = $("#jobTableforSen").find("tr");
		var sourceColumnSelected = false;
		trs.each(function(i, k) {
			var $tr = $(k);
			var json = {
				sc : "",
				tc : ""
			};
			json.sc = $tr.find(".source-third-select").val();
			
			json.defaultValue = $tr.find(".default-value").val();
			if (json.sc != '') {
				sourceColumnSelected = true;
			}
			json.tc = $tr.find(".target-column-name").val();
			arr.push(json);
		});
		if (!sourceColumnSelected) {
			layer.msg("请选择源表字段");
		}
		var jsonArr = JSON.stringify(arr);
		$("#columnRelation").val(jsonArr);
	}
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/save",
		data : $('#signupForm').serialize(),
		async : false,
		error : function(request) {
			laryer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("保存成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
}

/**
 * 校验
 * @returns
 */
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

/**
 * 
 * @param e
 * @param params
 * @returns
 */
$('.job-type-select').on('change', function() {
	var jobType = $("#jobType").val();
	if (3 == jobType) {
		$("#qiccClass").show();
		$("#taskClass").hide();
		$("#dataSync").hide();
		$("#jobTableforSen").empty();
		
		sourceColumnFlag = false;
		targetColumnFlag = false;
		
		loadTechnicalTable();
	} else if (2 == jobType) {
		$("#taskClass").hide();
		$("#qiccClass").hide();
		$("#dataSync").show();
		loadSourceTable();
		loadTargetTable();
		$("#jobTableforSen").empty();
		
		sourceColumnFlag = false;
		targetColumnFlag = false;
	} else {
		$("#taskClass").show();
		$("#dataSync").hide();
		$("#qiccClass").hide();
		$("#jobTableforSen").empty();
		
		sourceColumnFlag = false;
		targetColumnFlag = false;
	}
	$('.source-first-select').off('change').on('change', function() {
		loadSourceTable();
	});
	$('.target-first-select').off('change').on('change', function() {
		$("#jobTableforSen").empty();
		loadTargetTable();
	});
});

/**
 * 加载源数据表
 * @returns
 */
function loadSourceTable(){
	var sourceMetaType = $("#sourceMetaType").val();
	var html = '<option value="">--选择源表--</option>';
	$.ajax({
		url : prefix + '/getTable/'+sourceMetaType,
		success : function(data) {
			//加载数据
			$.each(data,function(key,value){
				html += '<option value="' + value.tableId + '">' 
							+ value.tableName +((value.tableComment == null || value.tableComment == "")?'' : ' ('+value.tableComment+') ')
						+ '</option>'
			})
			$(".source-second-select").html(html);
			$(".source-second-select").chosen('destroy').chosen({
				search_contains: true,
				maxHeight : 200
			});
			
			//动态加载 源数据字段信息
			$('.source-second-select').off('change').on('change', function(e, params) {
				loadSourceColumn();
			});
			
		}
	});
}

/**
 * 加载目标数据表
 * @returns
 */
function loadTargetTable(){
	var targetMetaType = $("#targetMetaType").val();
	var html = '<option value="">--选择目标表--</option>';
	$.ajax({
		url : prefix + '/getTable/'+targetMetaType,
		success : function(data) {
			//加载数据
			$.each(data,function(key,value){
				html += '<option value="' + value.tableId + '">' + value.tableName 
							+((value.tableComment == null || value.tableComment == "")?"":" ("+value.tableComment+") ")
						+ '</option>'
			})
				
			$(".target-second-select").html(html);
			$(".target-second-select").chosen('destroy').chosen({
				search_contains: true,
				maxHeight : 200
			});
			$('.target-second-select').off('change').on('change', function(e, params) {
				loadTargetColumn();
			});
		}
	});
}

var sourceColumnFlag = false;
var targetColumnFlag = false;

var sourceColumn;
var targetColumn;

/**
 * 加载源数据表字段信息
 * @returns
 */
function loadSourceColumn() {
	var sourceMetaType = $("#sourceMetaType").val();
	let tableId = $("#sourceTable").val();
	if(tableId == ""){
		$("#jobTableforSen").empty();
		return;
	}
	$.ajax({
		url : prefix + '/getColumn/'+sourceMetaType+"/"+tableId,
		success : function(data) {
			//加载数据
			sourceColumnFlag = true;
			sourceColumn = data;
			
			if( sourceColumnFlag  && targetColumnFlag ){
				var temp  = "";
				$.each(targetColumn,function(key1,value1){
					let defaultFlag = false;
					temp  +="<tr class='jobTr"+key1+"' >"
							+"<td>"+key1+"</td>"
							+"<td >"
							+"<select class='source-third-select' name='sourceColumnComment'>"
							+"<option value=\"\">--请选择字段--</option>";
					$.each(sourceColumn,function(key2,value2){
					temp  += '	<option data-column-comment="'+ value2.columnComment +'" value="' + value2.columnName + '" '+ (value1.columnComment == value2.columnComment?"selected=selected":"") +'>' + value2.columnName 
									+((value2.columnComment == null || value2.columnComment == "")?"":" ("+value2.columnComment+") ")
							+ '	</option>';
						if(value1.columnComment == value2.columnComment){
							defaultFlag = true;
						}
					})
					temp  += "</select>"
							+"</td>"
							+"<td>";
							if(defaultFlag){
					temp  += "-";			
							}else{
					temp  += "<input class='form-control default-value' type='text'/>";
							}
					temp  += "</td>"
							+"<td>"
							+"<input class='form-control target-column-name' value='"+ value1.columnName +"' readonly='readonly'>"
							+"</td>"
							+"<td>" + value1.columnComment
							+"</td>"
							+"</tr>";
				})
				$("#jobTableforSen").html(temp);
				$(".source-third-select").chosen({});
			}
		}
	});
}

/**
 * 加载目标数据表字段信息
 * @returns
 */
function loadTargetColumn() {
	$("#jobTableforSen").html('');
	var targetMetaType = $("#targetMetaType").val();
	let tableId = $("#targetTable").val();
	if(tableId == ""){
		$("#jobTableforSen").empty();
		return;
	}
	$.ajax({
		url : prefix + '/getColumn/'+targetMetaType+"/"+tableId,
		success : function(data) {
			//加载数据
			targetColumnFlag = true;
			targetColumn = data;
			
			if( sourceColumnFlag  && targetColumnFlag ){
				var temp  = "";
				$.each(targetColumn,function(key1,value1){
					let defaultFlag = false;
					temp  +="<tr class='jobTr"+key1+"' >"
							+"<td>"+key1+"</td>"
							+"<td >"
							+"<select class='source-third-select' name='sourceColumnComment'>"
							+"<option value=\"\">--请选择字段--</option>";
					$.each(sourceColumn,function(key2,value2){
					temp  += '	<option data-column-comment="'+ value2.columnComment +'" value="' + value2.columnName + '" '+ (value1.columnComment == value2.columnComment?"selected=selected":"") +'>' + value2.columnName 
									+((value2.columnComment == null || value2.columnComment == "")?"":" ("+value2.columnComment+") ")
							+ '	</option>';
						if(value1.columnComment == value2.columnComment){
							defaultFlag = true;
						}
					})
					temp  += "</select>"
							+"</td>"
							+"<td>";
							if(defaultFlag){
					temp  += "-";			
							}else{
					temp  += "<input class='form-control default-value' type='text'/>";
							}
					temp  += "</td>"
							+"<td>"
							+"<input class='form-control target-column-name' value='"+ value1.columnName +"' readonly='readonly'>"
							+"</td>"
							+"<td>" + value1.columnComment
							+"</td>"
							+"</tr>";
				})
				$("#jobTableforSen").html(temp);
				$(".source-third-select").chosen({});
			}
		}
	});
}

/**
 * 加载技术元数据表表
 * @returns
 */
function loadTechnicalTable(){
	var html = '<option value="">--选择技术表--</option>';
	$.ajax({
		url : prefix + '/getTable/1',
		success : function(data) {
			//加载数据
			$.each(data,function(key,value){
				html += '<option value="' + value.tableId + '">' + value.tableName 
							+((value.tableComment == null || value.tableComment == "")?"":" ("+value.tableComment+") ")
						+ '</option>'
			})
				
			$(".technical-select").html(html);
			$(".technical-select").chosen('destroy').chosen({
				search_contains: true,
				maxHeight : 200
			});
		}
	});
}